using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using TagLib;


namespace LANamp
{
    public class MusicInfo : object, IComparable
    {
        private string filename = "";
        private string filepath = "";
        private string artist = "";
        private string title = "";
        private TimeSpan length;
        private bool liveload;
        private bool unusable;

        public MusicInfo(string path)
        {
            this.liveload = true;
            Filepath = path;
        }

        public MusicInfo(bool liveload)
        {
            this.liveload = liveload;
        }

        public MusicInfo()
            : this(false)
        {
        }

        public MusicInfo(StreamReader reader)
        {
            this.liveload = false;
            this.filepath = reader.ReadLine();
            this.filename = reader.ReadLine();
            this.artist = reader.ReadLine();
            this.title = reader.ReadLine();
        }

        public int CompareTo(object obj)
        {
            if (obj is MusicInfo)
            {
                MusicInfo song = (MusicInfo)obj;
                return DisplayName.CompareTo(song.DisplayName);
            }
            throw new ArgumentException("object is not a MusicInfo object");
        }

        public void WriteToStream(StreamWriter writer)
        {
            writer.WriteLine(filepath);
            writer.WriteLine(filename);
            writer.WriteLine(artist);
            writer.WriteLine(title);
        }

        public override string ToString()
        {
            return DisplayName;
        }

        public string Filename
        {
            get
            {
                return filename;
            }
            set
            {
                if (!liveload) filename = value;
            }
        }

        public string Artist
        {
            get
            {
                return artist;
            }
            set
            {
                if (!liveload) artist = value;
            }
        }

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                if (!liveload) title = value;
            }
        }

        public string DisplayName
        {
            get
            {
                if ((artist == "") || (title == ""))
                    return filename;
                else
                    return artist + " - " + title;
            }
        }

        public string DisplayLength
        {
            get
            {
                return ((length.Ticks == 0) ? ("") : (String.Format("{00}:{00}", length.Minutes + (length.Hours * 60), length.Seconds)));
            }
        }

        public TimeSpan Length
        {
            get
            {
                return length;
            }
        }

        public bool Unusable
        {
            get
            {
                return unusable;
            }
        }
        public string Song
        {
            get
            {
                if ((artist == "") || (title == ""))
                    return null;
                else
                    return artist + " - " + title;
            }
        }
        public void ReadTag()
        {
                title = "";
                artist = "";
                TagLib.File file = null;                
                try
                {
                    file = TagLib.File.Create(filepath);
                }
                catch (TagLib.UnsupportedFormatException)
                {
                    Console.WriteLine ("UNSUPPORTED FILE: " + filepath);
                    unusable = true;
                    return;
                }
                catch (TagLib.CorruptFileException)
                {
                    Console.WriteLine("CORRUPTED FILE: " + filepath);
                    unusable = true;
                    return;
                }
                this.title = file.Tag.Title;
                this.artist = (file.Tag.AlbumArtists == null ? String.Empty : System.String.Join(" & ", file.Tag.AlbumArtists));
                if (file.Properties.MediaTypes != TagLib.MediaTypes.None)
                   this.length = file.Properties.Duration;
                // Console.Out.WriteLine(DisplayName+ " -=- " + Filepath);
        }

        public string Filepath
        {
            get
            {
                return filepath;
            }
            set
            {
                filepath = value;
                if (liveload)
                {
                    filename = (new FileInfo(filepath)).Name;

                }
            }
        }
    }
}
